<script setup>
import { ref, defineProps, defineEmits } from 'vue'

const props = defineProps({
  isEdit: {
    type: Boolean,
    default: false
  },
  initialData: {
    type: Object,
    default: () => ({
      title: '',
      state: '',
      machineNo: '',
      category: '',
      module: '',
      component: '',
      eventTime: '',
      updateTime: '',
      deadline: '',
      owner: '',
      recorder: '',
      description: '',
      progress: '',
      improvement: '',
      result: '',
      attachments: []
    })
  }
})

const emit = defineEmits(['submit', 'cancel'])

// 表单数据
const formData = ref({ ...props.initialData })

// 选项数据
const stateOptions = ref(['进行中', '已完成', '已关闭'])
const categoryOptions = ref(['硬件问题', '软件问题', '系统维护', '其他'])
const moduleOptions = ref(['模块A', '模块B', '模块C'])
const componentOptions = ref(['部件1', '部件2', '部件3'])

// 添加新选项
const addNewOption = (type, value) => {
  if (!value) return
  switch (type) {
    case 'category':
      if (!categoryOptions.value.includes(value)) {
        categoryOptions.value.push(value)
      }
      break
    case 'module':
      if (!moduleOptions.value.includes(value)) {
        moduleOptions.value.push(value)
      }
      break
    case 'component':
      if (!componentOptions.value.includes(value)) {
        componentOptions.value.push(value)
      }
      break
  }
}

// 处理文件上传
const handleFileUpload = (event) => {
  const files = Array.from(event.target.files)
  formData.value.attachments = [...formData.value.attachments, ...files]
}

// 移除附件
const removeAttachment = (index) => {
  formData.value.attachments.splice(index, 1)
}

// 提交表单
const handleSubmit = () => {
  emit('submit', formData.value)
}

// 取消
const handleCancel = () => {
  emit('cancel')
}
</script>

<template>
  <div class="daily-form">
    <h2>{{ isEdit ? '编辑问题反馈' : '新建问题反馈' }}</h2>
    
    <div class="form-content">
      <div class="form-row">
        <div class="form-item">
          <label>状态：</label>
          <div class="option-input">
            <select v-model="formData.state">
              <option value="">请选择状态</option>
              <option v-for="option in stateOptions" :key="option" :value="option">
                {{ option }}
              </option>
            </select>
          </div>
        </div>

        <div class="form-item">
          <label>机器编号：</label>
          <input type="text" v-model="formData.machineNo" placeholder="请输入机器编号">
        </div>
      </div>

      <div class="form-row">
        <div class="form-item">
          <label>类别：</label>
          <div class="option-input">
            <select v-model="formData.category">
              <option value="">请选择类别</option>
              <option v-for="option in categoryOptions" :key="option" :value="option">
                {{ option }}
              </option>
            </select>
            <button @click="addNewOption('category', prompt('请输入新的类别'))" class="add-option-btn">+</button>
          </div>
        </div>

        <div class="form-item">
          <label>模块：</label>
          <div class="option-input">
            <select v-model="formData.module">
              <option value="">请选择模块</option>
              <option v-for="option in moduleOptions" :key="option" :value="option">
                {{ option }}
              </option>
            </select>
            <button @click="addNewOption('module', prompt('请输入新的模块'))" class="add-option-btn">+</button>
          </div>
        </div>

        <div class="form-item">
          <label>部件：</label>
          <div class="option-input">
            <select v-model="formData.component">
              <option value="">请选择部件</option>
              <option v-for="option in componentOptions" :key="option" :value="option">
                {{ option }}
              </option>
            </select>
            <button @click="addNewOption('component', prompt('请输入新的部件'))" class="add-option-btn">+</button>
          </div>
        </div>
      </div>

      <div class="form-row">
        <div class="form-item">
          <label>发生时间：</label>
          <input type="datetime-local" v-model="formData.eventTime">
        </div>

        <div class="form-item">
          <label>更新时间：</label>
          <input type="datetime-local" v-model="formData.updateTime">
        </div>

        <div class="form-item">
          <label>截止时间：</label>
          <input type="datetime-local" v-model="formData.deadline">
        </div>
      </div>

      <div class="form-row">
        <div class="form-item">
          <label>责任所属：</label>
          <input type="text" v-model="formData.owner" placeholder="请输入责任所属">
        </div>

        <div class="form-item">
          <label>记录人员：</label>
          <input type="text" v-model="formData.recorder" placeholder="请输入记录人员">
        </div>
      </div>

      <div class="form-item full-width">
        <label>描述：</label>
        <textarea v-model="formData.description" rows="4" placeholder="请输入详细描述"></textarea>
      </div>

      <div class="form-item full-width">
        <label>进展：</label>
        <textarea v-model="formData.progress" rows="4" placeholder="请输入当前进展"></textarea>
      </div>

      <div class="form-item full-width">
        <label>改善措施：</label>
        <textarea v-model="formData.improvement" rows="4" placeholder="请输入改善措施"></textarea>
      </div>

      <div class="form-item full-width">
        <label>结果：</label>
        <textarea v-model="formData.result" rows="4" placeholder="请输入处理结果"></textarea>
      </div>

      <div class="form-item full-width">
        <label>附件：</label>
        <input type="file" @change="handleFileUpload" multiple accept="image/*,video/*,.zip,.rar">
        <div class="attachments-list" v-if="formData.attachments.length > 0">
          <div v-for="(file, index) in formData.attachments" :key="index" class="attachment-item">
            <span>{{ file.name }}</span>
            <button @click="removeAttachment(index)" class="remove-btn">删除</button>
          </div>
        </div>
      </div>

      <div class="form-actions">
        <button @click="handleSubmit" class="submit-btn">{{ isEdit ? '保存' : '发布' }}</button>
        <button @click="handleCancel" class="cancel-btn">取消</button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.daily-form {
  background-color: white;
  border-radius: 8px;
  padding: 2rem;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.form-content {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 1rem;
}

.form-item {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.form-item.full-width {
  grid-column: 1 / -1;
}

.form-item label {
  font-size: 0.9rem;
  color: #666;
}

.form-item input,
.form-item select,
.form-item textarea {
  padding: 0.5rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 0.9rem;
}

.option-input {
  display: flex;
  gap: 0.5rem;
}

.option-input select {
  flex: 1;
}

.add-option-btn {
  padding: 0.5rem;
  background-color: #0052cc;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
}

.attachments-list {
  margin-top: 0.5rem;
}

.attachment-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem;
  background-color: #f5f5f5;
  border-radius: 4px;
  margin-bottom: 0.5rem;
}

.remove-btn {
  padding: 0.25rem 0.5rem;
  background-color: #ff4d4f;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 1rem;
  margin-top: 1rem;
}

.submit-btn,
.cancel-btn {
  padding: 0.5rem 1.5rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: all 0.2s;
}

.submit-btn {
  background-color: #0052cc;
  color: white;
  border: none;
}

.submit-btn:hover {
  background-color: #0747a6;
}

.cancel-btn {
  background-color: white;
  color: #666;
  border: 1px solid #ddd;
}

.cancel-btn:hover {
  border-color: #0052cc;
  color: #0052cc;
}
</style>